/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.application.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Collection;
import java.util.List;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicList;
import org.toools.gwt.widget.client.bc.BasicListener;
import org.toools.gwt.widget.client.bc.BusinessObjectPanel;
import org.toools.gwt.widget.client.cloud.TagVO;
import org.toools.gwt.wmc.client.application.ApplicationServiceAsync;
import org.toools.gwt.wmc.jpa.Application;
import org.toools.gwt.wmc.jpa.CompanyPart;

/**
 *
 * @author manuel
 */
public class ApplicationControl extends BasicControl{

    public ApplicationControl(ApplicationServiceAsync service) {
        super(service, new org.toools.gwt.widget.client.util.Account());
        service.getApplications(new AsyncCallback<Collection<Application>>() {
                public void onSuccess(Collection<Application> result) {
                    System.out.println("Applications loaded");
                    TagVO[] tags = ApplicationControl.this.getTagVO(result.toArray());
                    ApplicationControl.this.setTags(tags);
                    ApplicationControl.this.clearStatus();
                    ApplicationControl.this.setStatus( ApplicationControl.this.getLoadedMessage());
                    ApplicationControl.this.getCreateList().clear();
                }

                public void onFailure(Throwable t) {
                    ApplicationControl.this.setStatus( "Failure on loading Applications");
                    ApplicationControl.this.getDefaultCreateTogglePanel().setVisible(false);
                    ApplicationControl.this.getCloudFilter().setVisible(false);
                }

                public String getOnFailureMessage(Throwable t) {
                    System.out.println("" + t);
                    return "Error while loading. Contact your administrator: " + new java.util.Date().getTime();
                }
        });
    }

    public ApplicationControl(ApplicationServiceAsync service, CompanyPart cp) {
        super(service, new org.toools.gwt.widget.client.util.Account());
        Collection<Application> applications = cp.getApplications();
        if (applications != null) {
            setTags(getTagVO(applications.toArray()));
        }
    }

    public BusinessObjectPanel createBasicPanel(Object o) {
        return new ApplicationPanel(o);
    }
    public BasicListener createBasicListener() {
        return new ApplicationListener(this);
    }
    public BasicList createBasicList() {
        ApplicationList al = new ApplicationList(new TagVO[]{}, this);
        return al;
    }
    public TagVO getTagVO(Object bp) {
        Application ap = (Application) bp;
        TagVO result = new TagVO("" + ap.getId(), ap.getContent(), 0, 0);
        result.setAttachedBusinessObject(ap);
        return result;
    }
    @Override
    public String getFilterMessage() {
        return "search by content";
    }
    @Override
    public String getTitle() {
        return "Applications";
    }
    @Override
    public void addSortItem() {
        getSortListBox().clear();
        getSortListBox().addItem("content");
    }
}
